********************************
** Create municipalities data **
********************************

import excel "municipalities_alldata.xlsx", firstrow clear

* change some names
replace municipality="Ostra goinge" if municipality=="Ostra Goinge"
replace municipality="Malung" if municipality=="Malung-Salen"

save "municip_combine.dta", replace

* create education variable
bysort municipality: gen post_secondary=post_sec + post_sec2

* create dummy for migration-office
replace migration_office=0 if migration_office!=1

* create dummy for no clicks
replace noclicks=0 if noclicks!=1

* create dummy for many SD
sum shareSDvotes
gen manySDv=1 if shareSDvotes>r(mean)
replace manySDv=0 if manySDv==.

* merge with SOM media data
merge 1:1 municipality using "SOMmedia.dta"
drop _merge

* merge with SOM trust data
merge 1:1 municipality using "SOMtrust.dta"
drop _merge

* merge with SOM opinion data 2014
merge 1:1 municipality using "SOM2014.dta"
drop _merge

* merge with SOM opinion data 2015
merge 1:1 municipality using "SOM2015.dta"
drop _merge

* use 2013/2014 data for 5 municipalities (missing for 2015 and 2014)
/* Bjurholm and Skinnskatteberg not in SOM, but also no clicks.*/
replace opinion15=1.67 if municipality=="Arjeplog"
replace opinion15=1.5 if municipality=="Sorsele"
replace opinion15=2 if municipality=="Hallefors"

* create dummy for pro-refugees (above mean)
sum opinion15
gen mopinion15=r(mean)
gen proopinion=1 if opinion15>mopinion15 
replace proopinion=0 if opinion15<mopinion15 
replace proopinion=. if opinion15==.

* create dummy for no dn use
bysort municipality: gen nodnuse=1 if oftena==0
replace nodnuse=0 if nodnuse!=1

* merge with 2016 SOM opinion data
merge 1:1 municipality using "SOM2016.dta"
drop _merge

save "final_municipalities.dta", replace
